import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Axios from 'axios'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    // route level code-splitting*
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    component: Home
  },
  {
    path: '/songSheetList',
    component: () => import('../components/SongSheetList.vue')
  },
  {
    path: '/singerList',
    component: () => import('../components/SingerList.vue')
  },
  {
    path: '/singerDetail',
    component: () => import('../components/SingerDetail.vue')
  },
  {
    path: '/songLeaderboards',
    component: () => import('../components/SongLeaderboards.vue')
  },
  {
    path: '/musicDetail',
    component: () => import('../components/MusicDetail.vue')
  },
  {
    path: '/musicSearch',
    component: () => import('../components/MusicSearch.vue')
  },
  {
    path: '/login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/register',
    component: () => import('../components/Register.vue')
  },
  {
    path: '/findPassword',
    component: () => import('../components/FindPassword.vue')
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const username = window.sessionStorage.getItem('username')

  if (!username) return next('/login')
  next()
})

export default router